<template>
	<!-- #ifndef APP-PLUS -->
	<view @click="selectAndUpload()">
		<slot></slot>
	</view>
	<!-- #endif -->
	<!-- #ifdef APP-PLUS -->
	<view @click="appStartREAD()">
		<slot></slot>
	</view>
	<!-- #endif -->
</template>

<script>
	// #ifdef APP-PLUS
	import permision from "@/js_sdk/wa-permission/permission.js"
	// #endif
	import UNI_APP from '@/.env.js'

	export default {
		name: "image-upload",
		data() {
			return {
				uploadHeaders: {
					"accessToken": uni.getStorageSync('loginInfo').accessToken
				}
			}
		},
		props: {
			maxCount: {
				type: Number,
				default: 1
			},
			maxSize: {
				type: Number,
				default: 5 * 1024 * 1024
			},
			sourceType: {
				type: String,
				default: 'album'
			},
			onBefore: {
				type: Function,
				default: null
			},
			onSuccess: {
				type: Function,
				default: null
			},
			onError: {
				type: Function,
				default: null
			},
		},
		methods: {
			// #ifdef APP-PLUS
			async requestAndroidPermission(permisionID) {
				var result = await permision.requestAndroidPermission(permisionID)
				var strStatus
				if (result == 1) {
					strStatus = "已获得授权"
					this.selectAndUpload()
					this.$emit('permissionRequestComplete', false)
				} else if (result == 0) {
					strStatus = "未获得授权"
					this.$emit('permissionRequestComplete', false)
				} else {
					uni.showModal({
						title: '提示',
						content: '未授权权限，无法使用功能，请在系统设置中打开权限',
						success: function(modalRes) {
							if (modalRes.confirm) {
								// 引导用户到设置页
								permision.gotoAppPermissionSetting()
							}
						}
					});
					// strStatus = "被永久拒绝权限,将跳转设置页面进行设置"
					// permision.gotoAppPermissionSetting()
					this.$emit('permissionRequestComplete', false)
				}
				if(result==0){
					uni.showModal({
						content: permisionID + strStatus,
						showCancel: false
					});
				}
			},
			appStartREAD(){
				if(this.sourceType=="camera"){
					this.requestAndroidPermission("android.permission.CAMERA")
				}else{
					this.requestAndroidPermission("android.permission.READ_EXTERNAL_STORAGE")
					this.requestAndroidPermission("android.permission.WRITE_EXTERNAL_STORAGE")
				}
				this.$emit('permissionRequestComplete', true)
			},
			// #endif
			selectAndUpload() {
				uni.chooseImage({
					count: this.maxCount, //最多可以选择的图片张数，默认9
					sourceType: [this.sourceType], //album 从相册选图，camera 使用相机，默认二者都有。如需直接开相机或直接选相册，请只使用一个选项
					sizeType: ['compressed'], //original 原图，compressed 压缩图，默认二者都有
					success: (res) => {
						res.tempFiles.forEach((file) => {
							console.log("文件:", file)
							if (!this.onBefore || this.onBefore(file)) {
								// 调用上传图片的接口
								this.uploadImage(file);
							}
						})
					}
				})
			},
			uploadImage(file) {
				uni.uploadFile({
					url: UNI_APP.BASE_URL + '/image/upload',
					header: {
						accessToken: uni.getStorageSync("loginInfo").accessToken
					},
					filePath: file.path, // 要上传文件资源的路径
					name: 'file',
					success: (res) => {
						uni.hideLoading()
						let data = JSON.parse(res.data);
						if (data.code != 200) {
							uni.showToast({
								icon: "none",
								title: data.message,
							})
							this.onError && this.onError(file, data);
						} else {
							this.onSuccess && this.onSuccess(file, data);
						}
					},
					fail: (err) => {
						uni.hideLoading()
						console.log(err);
						this.onError && this.onError(file, err);
					}
				})
			}
		}
	}
</script>

<style>
</style>